//
//  XYTagViewController.m
//  NBTripe
//
//  Created by Xy on 16/1/7.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "XYTagViewController.h"
#import "XYTagModel.h"

@interface XYTagViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) XYTagModel *model;

@end

@implementation XYTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
    [self.view addSubview:self.tableView];
    [self getTagData];
    [self navigationBar];
}

- (void)navigationBar {
    UILabel *label = [UILabel new];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.mas_equalTo(120);
        label.text = @"筛选&排序";
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:18];
    }];
    
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 100, self.view.frame.size.width, 25);
    view.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:0.5];
    [self.view addSubview:view];
    
    XyGradientView *views = [[XyGradientView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height -  XHIGHT * 200, self.view.frame.size.width, XHIGHT * 200) type:TRANSPARENT_GRADIENT_TYPE];
    [self.view addSubview:views];
    
}


#pragma mark - getTagData
- (void)getTagData {
    [XYNetWorkTool getWithUrl:@"http://api.breadtrip.com/hunter/products/tabs/filter/?city_name=%E5%8C%97%E4%BA%AC" parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        self.model = [[XYTagModel alloc] initWithDataSource:result];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
    }];
}


#pragma mark - heightView
- (void)heightView {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1.0];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake( XWIDTH * 15, XHIGHT * 15, self.view.frame.size.width, XHIGHT * 15)];
    lable.font = [UIFont systemFontOfSize:15];
    lable.text = @"选择标签";
    lable.textColor = [UIColor colorWithRed:0.604 green:0.9059 blue:0.8297 alpha:1.0];
    //第一个 label的起点
    CGSize size = CGSizeMake(10, 40);
    //间距
    CGFloat padding = XWIDTH * 20.0;
    CGFloat width = self.view.frame.size.width;
    for (int i = 0; i < self.model.tag_list.count; i ++) {
        //自适应宽度
        CGFloat keyWorldWidth = [XYNetWorkTool heightForCellWithText:self.model.tag_list[i][@"name"] Size:CGSizeMake(CGFLOAT_MAX, self.view.frame.size.height) fontSize:20].width;
        if (keyWorldWidth > width) {
            keyWorldWidth = width;
        }
        if (width - size.width < keyWorldWidth) {
            size.height += XHIGHT * 40;
            size.width = XWIDTH * 10.0;
        }
        //创建 label点击事件
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(size.width, size.height, keyWorldWidth + XWIDTH , XHIGHT *30)];
        button.titleLabel.numberOfLines = 0;
        button.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1.0];
//        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        button.layer.borderWidth = 1.0;
        button.layer.borderColor = [[UIColor whiteColor] CGColor];
        button.layer.cornerRadius = XHIGHT * 15.0;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitle:self.model.tag_list[i][@"name"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [view addSubview:button];
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonWithPress:) forControlEvents:UIControlEventTouchUpInside];
        //起点 增加
        size.width += keyWorldWidth+padding;
    }
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, size.height + (XHIGHT *  40));

    [view addSubview:lable];
    self.tableView.tableHeaderView = view;
}


- (void)buttonWithPress:(UIButton *)sender {

}


#pragma mark - TableView

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120*XHIGHT, self.view.frame.size.width, self.view.frame.size.height - (XHIGHT * 285)) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        _tableView.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CityTableViewCellIdentifier"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    self.model.tag_list.count + self.model.sorted_list.count
    return self.model.sorted_list.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return XHIGHT * 30;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, XHIGHT * 30);

    UILabel *lable = [UILabel new];
    lable.frame = CGRectMake(XWIDTH * 15, 0, view.frame.size.width - XHIGHT * 15, view.frame.size.height);
    lable.text = @"排序";
    lable.textColor = [UIColor colorWithRed:0.604 green:0.9059 blue:0.8297 alpha:1.0];
    [view addSubview:lable];
    
    view.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1.0];
    
    [self heightView];

    return view;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityTableViewCellIdentifier"];
    cell.textLabel.text = self.model.sorted_list[indexPath.row][@"name"];
    NSLog(@"%@", cell.textLabel.text);
    cell.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1.0];
    cell.textLabel.textColor = [UIColor whiteColor];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
